package org.example.jiaxiao.service.impl;

import org.example.jiaxiao.entity.Coach;
import org.example.jiaxiao.entity.Student;
import org.example.jiaxiao.mapper.CoachMapper;
import org.example.jiaxiao.service.CoachService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CoachServiceImpl implements CoachService {

    @Autowired
    private CoachMapper coachMapper;

    @Override
    public List<Coach> getAllCoaches() {
        return coachMapper.findAll();
    }

    @Override
    public Coach getCoachById(Integer coachId) {
        return coachMapper.findById(coachId);
    }

    @Override
    public int addCoach(Coach coach) {
        // 获取当前最大ID，自动生成新ID
        Integer maxId = coachMapper.findMaxId();
        coach.setCoachId(maxId == null ? 1 : maxId + 1);
        return coachMapper.insert(coach);
    }

    @Override
    public int updateCoach(Coach coach) {
        return coachMapper.update(coach);
    }

    @Override
    public int deleteCoach(Integer coachId) {
        return coachMapper.deleteById(coachId);
    }

    @Override
    public List<Student> getStudentsByCoachId(Integer coachId) {
        return coachMapper.findStudentsByCoachId(coachId);
    }
}
